<?php
namespace app\index\controller;

use app\common\controller\Frontend;
use think\Session;
use think\helper\Time;
use app\admin\model\QuesStudent;
use app\admin\model\Student;
use app\admin\model\QuesTeacher;
use app\admin\model\Teacher;
use app\admin\model\User;

class Ques extends Form
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'weiui';
    protected $model = null;
    protected $re_model = null;


    public function _initialize()
    {

        parent::_initialize();

    }

    public function index()
    {


        $title = '学生、教职工防疫信息摸底统计';
        // $openid = Session::get('wechatUser')['openid'];
        $openid = 'oMYPNwg_cHGJQh6vebU-FtZf04BM';
        echo $openid;
        $row1 = (new QuesStudent())->where(['openid'=>$openid])->find();
        $row2 = (new QuesTeacher())->where(['openid'=>$openid])->find();
        if($row1 || $row2)
            $title = '学生、教职工防疫信息日报统计';
        $this -> assign('pageTitle',$title);
        $this -> assign('title',$title);
        $this -> assign('wxcode','ques');

        if ($this->request->isPost()) {
            $openid = 'oMYPNwg_cHGJQh6vebU-FtZf04BM';
            $params = $this->request->post("row/a");
            $group_id = $this->request->post("group_id");
            if(!$group_id)
                $this->error('请选择用户类型！',null,$group_id);
            $where['nickname'] = $params['nickname'];
            $error_msg = '';
            switch ($group_id) {
                case '1':
                    $this->model = new QuesStudent();
                    $this->re_model = new Student();
                    $where['sid']= $params['sid'];
                    $error_msg = '学号、姓名填写有误,请检查！';
                    break;
                case '2':
                    $this->model = new QuesTeacher();
                    $this->re_model = new Teacher();
                    $where['tel']= $params['tel'];
                    $error_msg = '姓名、电话或工作填写有误,请检查！';
                    break;
                case '3':
                    $this->model = new \app\admin\model\QuesMerchant;
                    $this->re_model = new \app\admin\model\Merchant;
                    $where['tel']= $params['tel'];
                    $error_msg = '姓名、电话填写有误,请检查！';
                    break;
                default:
                    # code...
                    break;
            }

            $row = $this->re_model->where($where)->find();
            $params['type'] = $row -> type;
            // echo $this->re_model->getLastSql();
            if(!$row)
                $this->error($error_msg,null,0);

            $openid = Session::get('wechatUser')['openid'];


            switch ($group_id) {
                case '1':
                    if($row->classes)
                    {
                        $params['student_id']=$row->id;
                        $params['classes_id'] = $row->classes_id;
                        $params['classes_name'] = $row->classes->name;
                        $params['year_'] = $row->year_;
                        $params['college_id'] = $row->classes->college_id;
                        if($row->classes->college)
                            $params['college_name'] = $row->classes->college->name;

                    }
                    break;
                case '2':
                    if($row->classes)
                    {
                        $params['teacher_id']=$row->id;
                        $params['classes_id'] =  $row->classes->id ;
                        $params['classes_name'] = $row->classes->name;
                        // $params['year_'] = $row->year_;
                        $params['college_id'] = $row->classes->college_id;
                        if($row->classes->college)
                            $params['college_name'] = $row->classes->college->name;

                    }

                    break;
                case '3':
                    // $params['classes_id'] = $row->classes_id;
                    // $params['classes_name'] = $row->classes->name;
                    // $params['year_'] = $row->year_;
                    // $params['college_id'] = $row->classes->college_id;
                    // $params['college_name'] = $row->classes->college->name;
                    break;
                default:
                    # code...
                    break;
            }
//            var_dump($params);

            if(isset($params['lat']) && $params['lat'] && isset($params['lon']) && $params['lon']){
                $address = getAddress($params['lon'],$params['lat']);

                if($address){
                    $params['wx_formatted_address'] = $address['formatted_address'];
                    $params['wx_city'] = $address['city'];
                    $params['wx_province'] = $address['province'];
                    $params['wx_district'] = $address['district'];
                    $params['wx_street'] = $address['street'];
                }
            }

            $temp_arr = ['4','5','6','7','8'];
            if (in_array($params['a_9'], $temp_arr))
            {
               $params['a_4'] = 2;
            }
            $today = Time::today();
            $today_row = $this->model->where(['openid'=>$openid,'nickname'=>$params['nickname']])->whereTime('create_time','between',$today)->find();

            if($today_row)
            {
                $res = $today_row->allowField(true)->save($params);
                $this->success('上报成功','/student',0);
            }
            else
            {
                $params['openid'] = $openid;
                $res = $this->model->allowField(true)->save($params);
                if($group_id == 1)
                {
                    $row->is_reported = 1;
                    $row->save();
                }

                if ($res)
                    $this->success('上报成功','/student',0);
                else
                    $this->error('上报失败','/student',0);
            }

        }
        // $wxjs = $this -> app->jssdk->buildConfig(array('getLocation'), false);
        // $this -> assign('wxjs',$wxjs);
         return $this->view->fetch();
    }

    public function report()
    {

        $this -> redirect(url('/index/report/index'));

        $openid = Session::get('wechatUser')['openid'];
        // var_dump(Session::get('wechatUser'));
        // $openid = 'oMYPNwg_cHGJQh6vebU-FtZf04BM';
        // var_dump($openid);
        $user_row = (new User())->where(['openid'=>$openid])->find();
        // $this->assign('is_sub',$user['is_sub']);
        // $this-> assign ('is_sub',1);

        if($openid && $user_row && $user_row->g_id != 0)
        {

            if($user_row->g_id == 1)
                $row =  (new QuesStudent())->where(['openid'=>$openid])->order('id','desc')->find();
            if($user_row->g_id == 2)
                $row =  (new QuesTeacher())->where(['openid'=>$openid])->order('id','desc')->find();
            if($row)
            {
                $title = '学生、教职工防疫信息日报统计';
                $page = 'edit';
                $this -> assign('row',$row);
                $this -> assign('group_id',$user_row->g_id);
            }
           else{
                $title = '学生、教职工防疫信息摸底统计';
                $page = 'add';
           }

        }
        else
        {
            $title = '学生、教职工防疫信息摸底统计';
            $page = 'add';
        }
        $this -> assign('pageTitle',$title);
        $this -> assign('title',$title);
        $this -> assign('wxcode','ques');
        // $this->assign('is_sub',1);
         $from_storage = 0;
        if ($this->request->isPost()) {
            // var_dump($openid);
            $params = $this->request->post("row/a");
            $params['sid'] = strtoupper($params['sid']);
            $group_id = $this->request->post("group_id");
            if(!$group_id)
            {
                $this->error('请选择用户类型！',null,$group_id);
                // $from_storage = 1;
            }
            if(isset($params['a_10']) && $params['a_10'] == 0) {
                $params['a_10_1'] = 100;
                $params['a_10_2'] = 100;
            }

            $where['nickname'] = $params['nickname'];
            $error_msg = '';
            switch ($group_id) {
                case '1':
                    $this->model = new QuesStudent();
                    $this->re_model = new Student();
                    $where['sid']= $params['sid'];
                    $error_msg = '学号、姓名填写有误,请检查！';
                    break;
                case '2':
                    $this->model = new QuesTeacher();
                    $this->re_model = new Teacher();
                    $where['tel']= $params['tel'];
                    $error_msg = '姓名、电话或工作填写有误,请检查！';
                    break;
                case '3':
                    $this->model = new \app\admin\model\QuesMerchant;
                    $this->re_model = new \app\admin\model\Merchant;
                    $where['tel']= $params['tel'];
                    $error_msg = '姓名、电话填写有误,请检查！';
                    break;
                default:
                    # code...
                    break;
            }

            $per_row = $this->re_model->where($where)->find();
            // echo $this->re_model->getLastSql();
            if(!$per_row)
            {
                $this->error($error_msg,null,0);
                // $from_storage = 1;
            }

            $params['type'] = $per_row -> type;

            if(isset($params['lon']) && $params['lon'] && isset($params['lat']) && $params['lat']){
                $address = getAddress($params['lon'],$params['lat']);

                if($address){
                    $params['wx_formatted_address'] = $address['formatted_address'];
                    $params['wx_city'] = $address['city'];
                    $params['wx_province'] = $address['province'];
                    $params['wx_district'] = $address['district'];
                    // $params['street'] = $address['street'];
                }
            }


            $today = Time::today();
            $today_row = $this->model->where(['openid'=>$openid,'nickname'=>$params['nickname']])->whereTime('create_time','between',$today)->find();

            if($openid && $user_row)
            {
                $user_row->g_id = $group_id;
                $user_row->save();
            }

            if($today_row)
            {
                $res = $today_row->allowField(true)->save($params);
                $this->success('上报成功','',0);
            }
            else
            {
                $params['openid'] = $openid ? $openid:'';
                switch ($group_id) {
                    case '1':
                        if($per_row->classes)
                        {
                            $params['student_id']=$per_row->id;
                            $params['classes_id'] = $per_row->classes_id;
                            $params['classes_name'] = $per_row->classes->name;
                            $params['year_'] = $per_row->year_;
                            $params['college_id'] = $per_row->classes->college_id;
                            if($per_row->classes->college)
                                $params['college_name'] = $per_row->classes->college->name;

                        }
                        $per_row->is_reported = 1;
                        $per_row->save();
                        break;
                    case '2':
                        if($per_row->classes)
                        {
                            $params['teacher_id']=$per_row->id;
                            $params['classes_id'] =  $per_row->classes->id ;
                            $params['classes_name'] = $per_row->classes->name;
                            // $params['year_'] = $row->year_;
                            $params['college_id'] = $per_row->classes->college_id;
                            if($per_row->classes->college)
                                $params['college_name'] = $per_row->classes->college->name;

                        }

                        break;
                    case '3':
                        // $params['classes_id'] = $row->classes_id;
                        // $params['classes_name'] = $row->classes->name;
                        // $params['year_'] = $row->year_;
                        // $params['college_id'] = $row->classes->college_id;
                        // $params['college_name'] = $row->classes->college->name;
                        break;
                    default:
                        # code...
                        break;
                }


                $res = $this->model->allowField(true)->save($params);
                if ($res)
                    $this->success('上报成功','',0);
                else
                {
                    $this->error('上报失败','',0);
                    // $from_storage = 1;
                }

            }
        }

        $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this -> app->jssdk->setUrl($url);
        $wxjs = $this -> app->jssdk->buildConfig(array('getLocation'), false);
        $this -> assignConfig('wxjs',$wxjs);
        $this -> assign('wxjs',$wxjs);

        return $this->view->fetch($page);
    }

    public function report2()
    {

        $openid = Session::get('wechatUser')['openid'];
        if(!$openid)
            $this->error('获取不到openid');
        // var_dump(Session::get('wechatUser'));
        // $openid = 'oMYPNwg_cHGJQh6vebU-FtZf04BM';
        // var_dump($openid);
        $user_row = (new User())->where(['openid'=>$openid])->find();
        // $this->assign('is_sub',$user['is_sub']);
        // $this-> assign ('is_sub',1);

        if($openid && $user_row && $user_row->g_id != 0)
        {

            if($user_row->g_id == 1)
                $row =  (new QuesStudent())->where(['openid'=>$openid])->order('id','desc')->find();
            if($user_row->g_id == 2)
                $row =  (new QuesTeacher())->where(['openid'=>$openid])->order('id','desc')->find();
            if($row)
            {
                $title = '学生、教职工防疫信息日报统计';
                $page = 'edit';
                $this -> assign('row',$row);
                $this -> assign('group_id',$user_row->g_id);
            }
           else{
                $title = '学生、教职工防疫信息摸底统计';
                $page = 'add';
           }

        }
        else
        {
            $title = '学生、教职工防疫信息摸底统计';
            $page = 'add';
        }
        $this -> assign('pageTitle',$title);
        $this -> assign('title',$title);
        $this -> assign('wxcode','ques');
        // $this->assign('is_sub',1);
         $from_storage = 0;
        if ($this->request->isPost()) {
            // var_dump($openid);
            $params = $this->request->post("row/a");
            $params['sid'] = strtoupper($params['sid']);
            $group_id = $this->request->post("group_id");
            if(!$group_id)
            {
                $this->error('请选择用户类型！',null,$group_id);
                // $from_storage = 1;
            }

            $where['nickname'] = $params['nickname'];
            $error_msg = '';
            switch ($group_id) {
                case '1':
                    $this->model = new QuesStudent();
                    $this->re_model = new Student();
                    $where['sid']= $params['sid'];
                    $error_msg = '学号、姓名填写有误,请检查！';
                    break;
                case '2':
                    $this->model = new QuesTeacher();
                    $this->re_model = new Teacher();
                    $where['tel']= $params['tel'];
                    $error_msg = '姓名、电话或工作填写有误,请检查！';
                    break;
                case '3':
                    $this->model = new \app\admin\model\QuesMerchant;
                    $this->re_model = new \app\admin\model\Merchant;
                    $where['tel']= $params['tel'];
                    $error_msg = '姓名、电话填写有误,请检查！';
                    break;
                default:
                    # code...
                    break;
            }

             $per_row = $this->re_model->where($where)->find();
            // echo $this->re_model->getLastSql();
            if(!$per_row)
            {
                $this->error($error_msg,null,0);
                // $from_storage = 1;
            }
            $params['type'] = $per_row -> type;
            if(isset($params['lon']) && $params['lon'] && isset($params['lat']) && $params['lat']){
                $address = getAddress($params['lon'],$params['lat']);

                if($address){
                    $params['wx_formatted_address'] = $address['formatted_address'];
                    $params['wx_city'] = $address['city'];
                    $params['wx_province'] = $address['province'];
                    $params['wx_district'] = $address['district'];
                    // $params['street'] = $address['street'];
                }
            }


            $today = Time::today();
            $today_row = $this->model->where(['openid'=>$openid,'nickname'=>$params['nickname']])->whereTime('create_time','between',$today)->find();
            if($openid && $user_row)
            {
                 $user_row->g_id = $group_id;
                $user_row->save();
            }

            if($today_row)
            {
                $res = $today_row->allowField(true)->save($params);
                $this->success('上报成功','',0);
            }
            else
            {
                $params['openid'] = $openid ?$openid:'';
                switch ($group_id) {
                    case '1':
                        if($per_row->classes)
                        {
                            $params['student_id']=$per_row->id;
                            $params['classes_id'] = $per_row->classes_id;
                            $params['classes_name'] = $per_row->classes->name;
                            $params['year_'] = $per_row->year_;
                            $params['college_id'] = $per_row->classes->college_id;
                            if($per_row->classes->college)
                                $params['college_name'] = $per_row->classes->college->name;

                        }
                        $per_row->is_reported = 1;
                        $per_row->save();
                        break;
                    case '2':
                        if($per_row->classes)
                        {
                            $params['teacher_id']=$per_row->id;
                            $params['classes_id'] =  $per_row->classes->id ;
                            $params['classes_name'] = $per_row->classes->name;
                            // $params['year_'] = $row->year_;
                            $params['college_id'] = $per_row->classes->college_id;
                            if($per_row->classes->college)
                                $params['college_name'] = $per_row->classes->college->name;

                        }

                        break;
                    case '3':
                        // $params['classes_id'] = $row->classes_id;
                        // $params['classes_name'] = $row->classes->name;
                        // $params['year_'] = $row->year_;
                        // $params['college_id'] = $row->classes->college_id;
                        // $params['college_name'] = $row->classes->college->name;
                        break;
                    default:
                        # code...
                        break;
                }


                $res = $this->model->allowField(true)->save($params);
                if ($res)
                    $this->success('上报成功','',0);
                else
                {
                    $this->error('上报失败','',0);
                    // $from_storage = 1;
                }

            }
        }

        $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this -> app->jssdk->setUrl($url);
        $wxjs = $this -> app->jssdk->buildConfig(array('getLocation'), true);
        $this -> assignConfig('wxjs',$wxjs);
        $this -> assign('wxjs',$wxjs);

        return $this->view->fetch($page);
    }

}


